package com.base.hong.ui.main

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import com.base.hong.adapter.ChildFragmentAdapter
import com.base.hong.adapter.ViewPage2Adapter
import com.base.hong.databinding.FragmentProjectBinding
import com.base.hong.ui.base.BaseVMFragment
import com.base.hong.ui.main.viewmodel.HomeViewModel
import com.base.hong.ui.project.ChildProjectFragment
import com.base.hong.ui.project.viewmodel.ProjectViewModel
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.MODE_SCROLLABLE
import com.google.android.material.tabs.TabLayout.Mode
import com.google.android.material.tabs.TabLayoutMediator
import com.gyf.immersionbar.ImmersionBar
import com.htt.base_library.base.BaseFragment


/**
 * @ClassName MainExchangeFragment
 * @Description TODO
 * @Author Hong
 * @Date 2022/6/29 15:14
 * @Version 1.0
 */
class ProjectFragment : BaseVMFragment<ProjectViewModel, FragmentProjectBinding>() {

    var titleList = ArrayList<String>()
    override fun initView(view: View, savedInstanceState: Bundle?) {
        super.initView(view, savedInstanceState)
        ImmersionBar.with(this)
            .statusBarView(mViewBinding.toolbarLine)
            .statusBarDarkFont(true, 0.2f)
            .init()

    }

    override fun initData() {
        viewModel.getProjectTitle()
        viewModel.projectTitle.observe(this) {
            it?.forEach { item ->
                titleList.add(item.name)
            }
            val mAdapter = ChildFragmentAdapter(it, this.childFragmentManager, lifecycle)
            mViewBinding.viewpage2.adapter = mAdapter
            mViewBinding.tabLayout.tabMode = MODE_SCROLLABLE
            TabLayoutMediator(
                mViewBinding.tabLayout,
                mViewBinding.viewpage2
            ) { tab: TabLayout.Tab, position: Int ->
                tab.text = titleList[position]
            }.attach()
        }

    }
}